
//当选择一级菜单时
function selectonemenu(pm){
	var menus = document.getElementsByName("rolmenu");	
	for(var i=0;i<menus.length;i++){
		var id = menus[i].id;
		var pid = id.split("_")[0];//复选框id的第一位,父级菜单编号
		if(pid == pm.id){
			menus[i].checked = pm.checked;
		}
	}
}

//当选择而级菜单时(1.如果二级菜单有一个被选中，那么主菜单就必须被选中。2.如果二级菜单中任何一个没被选中，那么就不别选中)
function selectedchild(cm){
	//要得到二级菜单的主菜单的id编号
	var cmid = cm.id.split("_")[0];
	//当二级菜单任意一个被选中时，主菜单都被选中
	if(cm.checked == true){
		//使主菜单被选中
		var id = document.getElementById(cmid);
		id.checked = true;
	}else{
		//得到所有的菜单
		var clist = document.getElementsByName("rolmenu");
		//判断同组中是否所有的都被取消
		var flag = false;
		for(var i=0;i<clist.length;i++){
			var cid = clist[i].id;
			var temp = cid.split("_");
			//查找子菜单
			if(temp.length>1){//说明是子菜单
				if(temp[0] == cmid){
					if(clist[i].checked == true){
						flag = true;
					}else{
						clist[i].checked == false;
					}
				}
			}
			
		}		
		if(flag==false){
			var pmenu = document.getElementById(cmid);
			pmenu.checked = false;
		}else{
			var pmenu = document.getElementById(cmid);
			pmenu.checked = true;
		}		
	}
}

function saverolinfo(){
	//得到角色 
	var rid = document.getElementById('roleinfo.rid').value;	
	//删除该角色的权限
	//dwrijs.deleteMenuInfobyrid(rid,returnflag);	
}
function clickUpdate(rida,ridb,ridc,ridd){
	//把显示的值
	if(rida != "" && ridb != ""){
		document.getElementById(rida).style.display = "none";
		document.getElementById(ridb).style.display = "block";
	}
	if(ridc != "" && ridd != ""){
		document.getElementById(ridc).style.display = "none";
		document.getElementById(ridd).style.display = "block";
	}
}

function clickExist(rida,ridb,ridc,ridd){
	//把显示的值
	if(rida != "" && ridb != ""){
		document.getElementById(rida).style.display = "block";
		document.getElementById(ridb).style.display = "none";
	}
	if(ridc != "" && ridd != ""){
		document.getElementById(ridc).style.display = "block";
		document.getElementById(ridd).style.display = "none";
	}
}

//修改角色
var rid1;
var rid2;
var rid3;
var rid4;
var rname;
var id ;
var rnamehtml;
//更新角色名称
function updateRoleinfo(rid,rida,ridb,ridc,ridd,rolename){
	id = rid;
	rid1 = rida;
	rid2 = ridb;
	rid3 = ridc;
	rid4 = ridd;
	rnamehtml = rolename;
	rname = document.getElementById(rid+"_t").value;
	var regx = /^\s+|\s+$/g;
	rolename = rolename.replace(regx,"");
	rname = rname.replace(regx,"");
	//当角色名称已经改变了
	//通过角色名去查询是否存在
	//dwrroljs.juageRoleInfo(rname,rnameisexists);
	$.post("/webvote/role.do?action=updateRole",{"roleinfo.rid":rid,"roleinfo.rname":rname},function(text){
		if(text == "exists"){
			Ext.MessageBox.alert('系统提示',"此角色已存在");
			document.getElementById(id+"_t").value = rnamehtml;
		}else if(text == "success"){
			Ext.MessageBox.alert('系统提示',"更新成功！");
			document.getElementById(rid1).innerHTML = "";
			document.getElementById(rid1).innerHTML = rname;
			document.getElementById(rid1).style.display = "block";
			document.getElementById(rid2).style.display = "none";
			document.getElementById(rid3).style.display = "block";
			document.getElementById(rid4).style.display = "none";
			location.reload();//刷新网页
		}else{
			Ext.MessageBox.alert('系统提示',"更新失败！");
		}
	});
}

var rolename;
//添加角色
function addRoleInfo(){
	//得到角色名称
	var roleName = document.getElementById('rolename').value;
	if(roleName == ""){
		//alert("请输入角色名称进行添加！");
		Ext.MessageBox.alert('系统提示', '请输入角色名称进行添加！');
	}else{
	    //window.alert("将添加"+roleName+"，确定？");
	    Ext.MessageBox.confirm('添加角色：'+roleName,'确定，取消？',verify);
	}
	
}

function verify (btn){
    var roleName = document.getElementById('rolename').value;
    window.alert(roleName);
	if(btn == 'yes'){
		//通过角色名去查询是否存在
		$.post("/webvote/role.do?action=addRole",{"rname":roleName},function(text){
		    //window.alert("进入");
		  
			if(text == "exists"){
				Ext.MessageBox.alert("系统提示","此角色已经存在!");
				document.getElementById('rolename').select();
				document.getElementById('rolename').focus();
			}else if(text == "success"){
				Ext.MessageBox.alert('系统提示',"添加成功！");
				//location.reload();//刷新网页
			}else{
				Ext.MessageBox.alert('系统提示',"添加失败！");
			}
		});
	}else{
		Ext.MessageBox.alert('系统提示','您取消了本次任务！');
		}
	
}

//删除角色
var rids='';   //角色的id集
function deleteRoleInfo(){

 var selectFlags  = document.getElementsByName("selectFlag");
 var flag = false;
	for (var i=0; i<selectFlags.length; i++) {
		if (selectFlags[i].checked) {
		    flag=true;
			//window.alert(selectFlags[i].id);
			rids+=selectFlags[i].id+',';
			//window.alert(rids);
		}
	}
	//如果没有选择用户
	if (!flag) {
		Ext.MessageBox.alert('系统提示', '请选择需要删除的角色！');
		}
	 else{
	 	 Ext.MessageBox.confirm('将删除选中角色','确定，取消？',verifydel);
	 }
}

function verifydel (btn){
  if(btn == 'yes'){
  $.post("/webvote/role.do?action=deleteRole",{"rids":rids},function(text){
		    //window.alert("进入");
		   if(text == "success"){
				Ext.MessageBox.alert('系统提示',"删除成功！");
				location.reload();//刷新网页
			}else{
				Ext.MessageBox.alert('系统提示',"删除失败！");
			}
		});
	}
	else{
		Ext.MessageBox.alert('系统提示','您取消了本次任务！');
		}
}


//全选或全不选
function checkAll(field) {
	var selectFlags = document.getElementsByName("selectFlag");
	for (var i=0; i<selectFlags.length; i++) {
		selectFlags[i].checked = field.checked;
	}
}


